<?php
include_once 'config.php';
include_once 'Controlador/CargaMasiva.php';

abstract class Index{
	
	public static function main(){	
		if(empty($_POST) && empty($_GET)){
			include_once 'Vista/UI_Contenido.php';
			self::Start();
			UI_Contenido::show();
		}
		elseif (isset($_POST['fcnAjax'])){
			self::AjaxControl($_POST['fcnAjax']);				
		}
	}	
	
	private function AjaxControl($fcnAjax){
		switch ($fcnAjax) {
			case 'seleccionar':
				if(isset($_POST['getData'])){
					include_once 'Controlador/AjaxResponse.php';
					include_once 'Modelo/DA_BD_Reporte.php';
					$result=false;
					switch ($_POST['getData']) {
						case 'apps':
							if(isset($_POST['dateConfig']) && isset($_POST['date'])){									
								$result = DA_BD_Reporte::selectAppsByDate($_POST['dateConfig'],$_POST['date']);
								$contentType = AjaxResponse::typeApp;
							}			
						break;
						case 'win':
							if(isset($_POST['dateConfig']) && isset($_POST['date']) && isset($_POST['idApp'])){									
								$result = DA_BD_Reporte::selectWinsByDateByApp($_POST['dateConfig'],$_POST['date'],$_POST['idApp']);
								$contentType = AjaxResponse::typeWin;
							}			
						break;
						default:
							return '';
						break;
					}
					if($result!==false){
						$objAjaxResponse = new AjaxResponse($contentType,$result['error'],$result['rows']);		
						$objAjaxResponse->send();	
					}
					else{
						echo '';
					}						
				}								
			break;
			case 'cargaMasiva':
					include_once 'Vista/UI_Apariencia.php';
					CargaMasiva::iniciarProceso();					
			break;
			case 'destroyDB':
					include_once 'Modelo/BD_MySQL.php';
					$bd = BD_MySQL::getInstance();
					$bd->connect();
					$bd->truncateTable('reportelinea');
					$bd->truncateTable('ventana');
					$bd->truncateTable('aplicacion'); 
					$bd->truncateTable('reporte');
					if(!$bd->hayError()){
						include_once 'Vista/UI_Contenido.php';
						UI_Contenido::destructionComplete();
					}
					else {
						include_once 'Vista/UI_Error.php';
						UI_Error::imprimeError($bd->getError());
					}
					$bd->close();				
			break;				
		}
	}
	
	private static function Start(){
		if(Omicron::debugMode){
			ini_set('display_errors', 'On');
			error_reporting(E_ALL);	
		}
		if(Omicron::sendGzipContent){
			if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
				ob_start("ob_gzhandler");	
			}
			else{
				ob_start();	
			}
		}
	}	
}

Index::main();

?>